package com.zyw.module.rest;

import com.zyw.mvc.servlet.BaseController;
import com.zyw.mvc.support.WrappedResult;
import com.zyw.security.shiro.stateless.realm.StatelessToken;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zhangyw
 * @date 2017/12/18 17:18
 */
@RestController
@RequestMapping("${path.rest.prefix}")
public class BasicGatewayController extends BaseController {


    /**
     * 接口登录认证
     * @param username
     * @param password
     * @return
     */
    @RequestMapping(value = "login", method = RequestMethod.POST)
    @ResponseBody
    public WrappedResult _login(String username, String password) {
        StatelessToken statelessToken = new StatelessToken();
        statelessToken.setUsername(username);
        statelessToken.setPassword(password);

        Subject subject = SecurityUtils.getSubject();
        subject.login(statelessToken);

        return wrappedSuccessResult("", "登录成功");
    }

    @RequestMapping(value = "logout", method = RequestMethod.POST)
    @ResponseBody
    public WrappedResult _logout(String username) {

        Subject subject = SecurityUtils.getSubject();
        subject.logout();

        return wrappedSuccessResult("", "登出成功");
    }

//    @RequestMapping("/**")
//    public void gateway(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        String reqUrl = request.getRequestURI();
//        request.getRequestDispatcher(reqUrl.replace("/wx/gateway","")).forward(request,response);
//    }
}
